package com.ruoyi.accounting.controller;

import com.ruoyi.accounting.domain.AccAccountBook;
import com.ruoyi.accounting.service.AccAccountBookService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 账本Controller
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@RestController
@RequestMapping("/book")
public class AccAccountBookController extends BaseController {
    @Autowired
    private AccAccountBookService accAccountBookService;

    /**
     * 查询账本列表
     */
    @RequiresPermissions("accounting:book:list")
    @GetMapping("/list")
    public TableDataInfo list(AccAccountBook accAccountBook) {
        startPage();
        List<AccAccountBook> list = accAccountBookService.selectAccAccountBookList(accAccountBook);
        return getDataTable(list);
    }

    /**
     * 导出账本列表
     */
    @RequiresPermissions("accounting:book:export")
    @Log(title = "账本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccAccountBook accAccountBook) {
        List<AccAccountBook> list = accAccountBookService.selectAccAccountBookList(accAccountBook);
        ExcelUtil<AccAccountBook> util = new ExcelUtil<AccAccountBook>(AccAccountBook.class);
        util.exportExcel(response, list, "账本数据");
    }

    /**
     * 获取账本详细信息
     */
    @RequiresPermissions("accounting:book:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(accAccountBookService.selectAccAccountBookById(id));
    }

    /**
     * 新增账本
     */
    @RequiresPermissions("accounting:book:add")
    @Log(title = "账本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccAccountBook accAccountBook) {
        accAccountBook.setCreateBy(SecurityUtils.getUsername());
        return toAjax(accAccountBookService.insertAccAccountBook(accAccountBook));
    }

    /**
     * 修改账本
     */
    @RequiresPermissions("accounting:book:edit")
    @Log(title = "账本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccAccountBook accAccountBook) {
        return toAjax(accAccountBookService.updateAccAccountBook(accAccountBook));
    }

    /**
     * 删除账本
     */
    @RequiresPermissions("accounting:book:remove")
    @Log(title = "账本", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(accAccountBookService.deleteAccAccountBookByIds(ids));
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("accounting:book:edit")
    @Log(title = "账本管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody AccAccountBook accAccountBook) {
        AccAccountBook oldBook = accAccountBookService.selectAccAccountBookById(accAccountBook.getId());
        if (Objects.isNull(oldBook)) {
            return AjaxResult.error("账本不存在");
        }

        if (oldBook.getAccountBookStatus().equals(accAccountBook.getAccountBookStatus())) {
            return AjaxResult.error("状态未改变");
        }

        oldBook.setUpdateTime(DateUtils.getNowDate());
        oldBook.setUpdateBy(SecurityUtils.getUsername());
        oldBook.setAccountBookStatus(accAccountBook.getAccountBookStatus());
        return toAjax(accAccountBookService.updateBookStatus(oldBook));
    }

}
